package document;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;

public class Disk {
	public static final int RECORD_SIZE = 90;
	public static final int BLOCK_SIZE = 90 * 8;
	public static final int FAT_SIZE = 4 * 128;

	public static void test() {
		
		
		//判断该disk文件是否存在，若没有则创建
		File file=new File("disk");    
		if(!file.exists())    
		{    
		    try {    
		         Disk disk=new Disk();
		         disk.InitDisk();
		    	
		    } catch ( Exception e) {    
		        // TODO Auto-generated catch block    
		        e.printStackTrace();    
		    }    
		}    
		 
		Disk disk=new Disk();
		ManagingCommonFile mc = new ManagingCommonFile();
		ManagingDirectory md = new ManagingDirectory();
		int fat[]=new int[128];
		 
	    //看磁盘使用情况
		/*fat=disk.inputDiskToFat();
		for(int i:fat){
			System.out.println(i);
		}
		 */
		
	 	//md.createADirectory ("Root/a",1);
	 	// mc.createACommonFile("Root/a/f",52);
 	// md.seeADirContent("Root");
 	//md.deleteADir("Root/a");
 
  	 //mc.deleteAFile ("Root/f" );
	 	/*mc.seeAFile("Root/a/f");
 Record[] records=disk.readRecordsFromABlock(8);
 for(Record record:records){
	 System.out.println(record);
 }*/
		 
	
	}

	public void inputFatToDisk(FileAllocationTable fat) {
		try {

			RandomAccessFile out = new RandomAccessFile(
					"disk", "rw");
			fat.writeData(out);
			 
			
			out.close();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	}

	public int[] inputDiskToFat() {
		
		FileAllocationTable newFat=new FileAllocationTable();
		int[] fat=new int[128];
		try {
			RandomAccessFile in = new RandomAccessFile(
					"disk", "r");
			newFat.readData(in);
			in.close();
			
			fat = newFat.getFat();
			 
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return fat;
	}

	public void writeADirRecoredToDisk(Record dirRecord, int index) {
		try {
			RandomAccessFile out = new RandomAccessFile(
					"disk", "rw");
			out.seek(index); // 找到记录块的位置
			
			 
			
			dirRecord.writeData(out);
			out.close();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	}

	public Record readADirRecordFromDisk(int index) {
		Record newDirectryRecord = null;
		try {
			RandomAccessFile in = new RandomAccessFile(
					"disk", "r");
			newDirectryRecord = new Record();
			in.seek(index);
			newDirectryRecord.readData(in);
			in.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return newDirectryRecord;
	}

	
	//初始化某个盘块
	public void initDirBlock(int blockNumber) {

		Record directoryRecord;
        
		int index=4 * 128 + 8 * (blockNumber - 2) * RECORD_SIZE;
		 
		directoryRecord = new Record(" ", ' ', 0, 0);
		try {
			RandomAccessFile out = new RandomAccessFile(
					"disk", "rw");
			
			out.seek(index); // 找到记录块的位置
			
			for (int i = 0; i < 8; i++) {
				directoryRecord.writeData(out);
			}
			out.close();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}

	}

	 
	
	public Record[] readRecordsFromABlock(int  BlockNumber) {

	 
		int index=FAT_SIZE+BLOCK_SIZE*(BlockNumber-2);
		Record[] records = new Record[8];
		
		try {
			RandomAccessFile in = new RandomAccessFile(
					"disk", "r");
            in.seek(index);
			
			for (int i = 0; i < 8; i++) {
				records[i] = new Record(" ",' ',0,0 );
 
				records[i].readData(in);
				
				
				 				 
			}
			in.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block

		}	 
        return records;
		 
	}
	
	 
	public int findANullRecord(int  BlockNumber) {
 
		int index=FAT_SIZE+BLOCK_SIZE*(BlockNumber-2);
		Record record=new Record();
		int  i=8; 
		try {
			RandomAccessFile in = new RandomAccessFile(
					"disk", "r");
            in.seek(index);
			
			for (  i = 0; i < 8; i++) {
				record.readData(in);
				if(record.getName().equals(" "))
				break;
				 			 
			}
			in.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
		}	
		 
        return  i;
	}
	 
	//判断文件是否存在，初始化disk
	public void InitDisk(){
		// Disk disk = new Disk();
		FileAllocationTable fat = new FileAllocationTable();
		int[] data=new int[128];
		for(int i=0;i<3;i++){
			data[i]=255;
		}
		for(int i=3;i<128;i++){
			data[i]=0;
		}
		fat.setFat(data);
		inputFatToDisk(fat);
		
		 initDirBlock(2);
	     inputDiskToFat();
		 
		
	}
	
	
	  /*在某个记录块中找到要找的名字，是文件夹名的记录的话，
	返回其霸占的块号*/
	public int searchARecordOfDir(int blockNumber, String name) {
        int dnumber = 255;
		  
		Record[] records = new Record[8];
		 
		records = readRecordsFromABlock(blockNumber);

		
		for (Record record : records) {
        
			try {
				if (record.getAttribute()==' '&&record.getName().equals(name)) {
					// 返回record的磁盘块号
					dnumber = record.getDnum();
					break;
				}
			} catch (Exception e) {
				// 找不到，不提示错误
			}
			dnumber = 255;
		} 
       

        
		return dnumber;
		 
	}

	/*在某个记录块中找到要找的名字， 
	 若是文件的记录的话，返回其磁盘首块号	*/
	public int searchARecordOfFile(int blockNumber, String name){
		 int dnumber = 255;
		  
			Record[] records = new Record[8];
			 
			records = readRecordsFromABlock(blockNumber);

			
			for (Record record : records) {
	        
				try {
					if (record.getAttribute()=='e'&&record.getName().equals(name)) {
						// 返回record的磁盘块号
						dnumber = record.getDnum();
						break;
					}
				} catch (Exception e) {
					// 找不到，不提示错误
				}
				dnumber = 255;
			} 
	       

	        
			return dnumber;
			 
	}
	
	
	public int resetFileRecordFromABlock(int  BlockNumber,String name) {

	    int dir=0;
		int index=FAT_SIZE+BLOCK_SIZE*(BlockNumber-2);
		Record[] records = new Record[8];
		
		try {
			RandomAccessFile in = new RandomAccessFile(
					"disk", "rw");
            in.seek(index);
			
			for (int i = 0; i < 8; i++) {
				records[i] = new Record(" ",' ',0,0 );
				records[i].readData(in);
				if(records[i].getName().equals(name)&&records[i].getAttribute()=='e'){
					dir=records[i].getDnum();
					records[i] = new Record(" ",' ',0,0 );
					int index1=(int) (in.getFilePointer()-90);
					in.seek(index1);
					records[i].writeData(in);
					break;
				}
				 				 
			}
			in.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block

		}
		return dir;	 
         
	}
	
	public int resetDirRecordFromABlock(int  BlockNumber,String name) {

	    int dir=0;
		int index=FAT_SIZE+BLOCK_SIZE*(BlockNumber-2);
		Record[] records = new Record[8];
		
		try {
			RandomAccessFile in = new RandomAccessFile(
					"disk", "rw");
            in.seek(index);
			
			for (int i = 0; i < 8; i++) {
				records[i] = new Record(" ",' ',0,0 );
				records[i].readData(in);
				if(records[i].getName().equals(name)&&records[i].getAttribute()==' '){
					dir=records[i].getDnum();
					records[i] = new Record(" ",' ',0,0 );
					int index1=(int) (in.getFilePointer()-90);
					in.seek(index1);
					records[i].writeData(in);
					break;
				}
				 				 
			}
			in.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block

		}
		return dir;	 
         
	}
	
	
	
	
	public Record seeRecordFromABlock(int  BlockNumber,String name) {

		 
		int index=FAT_SIZE+BLOCK_SIZE*(BlockNumber-2);
		Record[] records = new Record[8];
		Record record=null;
		
		try {
			RandomAccessFile in = new RandomAccessFile(
					"disk", "rw");
            in.seek(index);
			
			for (int i = 0; i < 8; i++) {
				records[i] = new Record(" ",' ',0,0 );
				records[i].readData(in);
				if(records[i].getName().equals(name)){
					System.out.print("该文件是：");
					 System.out.println(records[i]);
					 record=records[i];
					break;
				}
				else{
					System.out.println("找不到该文件！");
				}
				 				 
			}
			in.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block

		}
		return record;	 
        
		 
	}
	
	
	
}
